//
//  HomePageHeaderView.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/7/28.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "HomePageHeaderView.h"

@interface HomePageHeaderView ()

@property (weak, nonatomic) IBOutlet UILabel *label;

@property (weak, nonatomic) IBOutlet UILabel *usdTotalValue;
@property (weak, nonatomic) IBOutlet UILabel *lastTime;
@property (weak, nonatomic) IBOutlet UIView *chartBgView;

@property (nonatomic, strong) UIView * chartView;
@property (nonatomic, strong) NSArray<NSNumber *> *borderWidthArray;
@property (nonatomic, strong) NSArray<UIColor *> *colorArray;
@property (nonatomic) NSArray *endPercentages;

@end

@implementation HomePageHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.chartView = [[UIView alloc]initWithFrame:CGRectMake(57, 0, 140, 140)];
    self.chartView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    self.chartView.layer.cornerRadius = 70;
    [self.chartBgView addSubview:self.chartView];
    
    self.label.text = JFMainLocalizedString(@"myAssets.h2");
    
}

- (void)setWalletsEntity:(WalletsEntity *)walletsEntity {
    _walletsEntity = walletsEntity;
    
    NSString *total = @(self.walletsEntity.digital.usdTotalValue.doubleValue + self.walletsEntity.hkAccount.usdTotalValue.doubleValue + self.walletsEntity.usAccount.usdTotalValue.doubleValue + self.walletsEntity.sgAccount.usdTotalValue.doubleValue).stringValue;
    if (total.doubleValue != 0) {
        self.usdTotalValue.text = [NSString stringWithFormat:@"$ %@", MoneyFormat(total)];
        
        self.lastTime.text = [NSString stringWithFormat:@"%@%@", JFMainLocalizedString(@"myAssets.last_updated"), walletsEntity.last_updated];
        
        self.dataValueArray = @[@(self.walletsEntity.hkAccount.usdTotalValue.doubleValue + self.walletsEntity.usAccount.usdTotalValue.doubleValue + self.walletsEntity.sgAccount.usdTotalValue.doubleValue), @(0), @(self.walletsEntity.digital.usdTotalValue.doubleValue), @(0), @(0), @(0)];
    }else {
        self.chartView.hidden = YES;
    }
}

- (void)setDataValueArray:(NSArray<NSNumber *> *)dataValueArray {
    _dataValueArray = dataValueArray;
    
    __block CGFloat currentTotal = 0;
    NSMutableArray *endPercentages = [NSMutableArray new];
    CGFloat total = [[self.dataValueArray valueForKeyPath:@"@sum.floatValue"] floatValue];
    
    [dataValueArray enumerateObjectsUsingBlock:^(NSNumber *value, NSUInteger idx, BOOL *stop) {
//        if (value.doubleValue == 0){
//            [endPercentages addObject:@(1.0 / dataValueArray.count * (idx + 1))];
//        }else{
            currentTotal += value.doubleValue;
            [endPercentages addObject:@(currentTotal / total)];
//        }
    }];
    self.endPercentages = [endPercentages copy];
    
    [self updateChartView];
}

- (void)updateChartView {
    [self.chartView.layer.sublayers enumerateObjectsUsingBlock:^(__kindof CALayer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperlayer];
    }];
    
    CGFloat _innerCircleRadius = 22;
    
    for (int i = 0; i < 6; i++) {
        CGFloat startPercentage = [self startPercentageForItemAtIndex:i];
        CGFloat endPercentage   = [self endPercentageForItemAtIndex:i];
        
        CGFloat borderWidth = [self.borderWidthArray[i] floatValue];
        CGFloat radius =  _innerCircleRadius + (borderWidth) / 2;;
        
        CAShapeLayer *currentPieLayer =    [self newCircleLayerWithRadius:radius
                                                           borderWidth:borderWidth
                                                             fillColor:[UIColor clearColor]
                                                           borderColor:self.colorArray[i]
                                                       startPercentage:startPercentage
                                                         endPercentage:endPercentage];
        [self.chartView.layer addSublayer:currentPieLayer];
        
        UIView *colorBgView = [[UIView alloc]initWithFrame:CGRectMake(257, 22 + 16 * i, 10, 10)];
        colorBgView.backgroundColor = UIColorFromHex(0xffffff);
        colorBgView.layer.cornerRadius = 5;
        [self.chartBgView addSubview:colorBgView];
        
        UIView *colorView = [[UIView alloc]initWithFrame:CGRectMake(258, 23 + 16 * i, 8, 8)];
        colorView.backgroundColor = self.colorArray[i];
        colorView.layer.cornerRadius = 4;
        [self.chartBgView addSubview:colorView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(colorView.frame) + 7, CGRectGetMinY(colorView.frame) - 2, SCREEN_WIDTH - CGRectGetMaxX(colorView.frame) - 7, 12)];
//        label.text =  @[@"Currencies", @"Insurance", @"Digital", @"Securities", @"Commodities", @"Alternatives"][i];
        label.text =  @[JFMainLocalizedString(@"currencies"), JFMainLocalizedString(@"insurance"), JFMainLocalizedString(@"digital"), JFMainLocalizedString(@"securities"), JFMainLocalizedString(@"commodities"), JFMainLocalizedString(@"alternatives")][i];
        label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
        label.textColor = UIColorFromHex(0xFFFFFF);
        [self.chartBgView addSubview:label];
    }
}

- (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index{
    if(index == 0){
        return 0;
    }
    
    return [_endPercentages[index - 1] floatValue];
}

- (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index{
    return [_endPercentages[index] floatValue];
}

- (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius
                               borderWidth:(CGFloat)borderWidth
                                 fillColor:(UIColor *)fillColor
                               borderColor:(UIColor *)borderColor
                           startPercentage:(CGFloat)startPercentage
                             endPercentage:(CGFloat)endPercentage{
    CAShapeLayer *circle = [CAShapeLayer layer];
    
    CGPoint center = CGPointMake(CGRectGetMidX(self.chartView.bounds),CGRectGetMidY(self.chartView.bounds));
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center
                                                        radius:radius
                                                    startAngle:-M_PI_2
                                                      endAngle:M_PI_2 * 3
                                                     clockwise:YES];
    
    circle.fillColor   = fillColor.CGColor;
    circle.strokeColor = borderColor.CGColor;
    circle.strokeStart = startPercentage;
    circle.strokeEnd   = endPercentage;
    circle.lineWidth   = borderWidth;
    circle.path        = path.CGPath;
    
    return circle;
}


- (IBAction)displayAction:(UIButton *)sender {
    if (self.chartView.hidden) {
        return;
    }
    
    sender.selected = !sender.isSelected;
    
    self.height = sender.isSelected ? 280 : 110;
    if (self.displayAction) {
        self.displayAction();
    }
}

- (NSArray<NSNumber *> *)borderWidthArray {
    if (!_borderWidthArray) {
        _borderWidthArray = @[@(40), @(25), @(35), @(20), @(30), @(10)];
    }
    return _borderWidthArray;
}

- (NSArray<UIColor *> *)colorArray {
    if (!_colorArray) {
        _colorArray = @[UIColorFromHex(0xB988B8), UIColorFromHex(0x70896A), UIColorFromHex(0xF8A728), UIColorFromHex(0x89C979), UIColorFromHex(0x29BDCF), UIColorFromHex(0xF47EA3)];
    }
    return _colorArray;
}


@end
